#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/ide.h>
#include <linux/delay.h>
#include <linux/cdev.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>

/*

	backlight {
		compatible = "pwm-backlight";
		pwms = <&pwm1 0 5000000>;
		brightness-levels = <0 4 8 16 32 64 128 255>;
		default-brightness-level = <6>;
		status = "okay";
	};

*/

static int __init dtsof_init(void)
{
    int ret = 0;
    struct device_node * bl_nd = NULL;
    struct property * nd_prop = NULL;
    const char * stachar = NULL;
    u32 out_value = 0;
    u32 *out_values = NULL;
    int i = 0,j = 0;
    /*  1: 找到backlight节点，路径是:/backlight  */ 
    bl_nd = of_find_node_by_path("/backlight");
    if(bl_nd == NULL) {
        ret = -1;
        return ret;
    }

    /* 获取compatible属性 */
    nd_prop = of_find_property(bl_nd,"compatible",NULL);
    if(nd_prop == NULL) {
        ret = -1;
        return ret;
    }
    else {
        printk("compatible = %s \r\n",(char *)nd_prop->value);
    }

    /* 获取字符串属性 */
    ret = of_property_read_string(bl_nd,"status",&stachar);
    if (ret <0)
    {
        ret = -1;
        return ret;
    }else {
        printk("status = %s \r\n",stachar);
    }       
    
    /* 获取数字属性 */
    ret = of_property_read_u32(bl_nd,"default-brightness-level",&out_value);
    if(ret < 0){
        ret = -1;
        return ret;
    }else {
        printk("default-brightness-level = %d \r\n",out_value);
    }

    /* 数组型数据 */
    i = of_property_count_u32_elems(bl_nd,"brightness-levels");
    out_values = kmalloc(i*sizeof(u32),out_values);
    ret = of_property_read_u32_array(bl_nd,"brightness-levels",out_values,i);
    if (ret < 0)    
    {
        printk("ret = %d \r\n",ret);
        return ret;
    }else {
        printk("i = %d \r\n",i);
        printk("brightness-levels= ");
        for(j=0;j<i;j++) 
            printk("%d ",out_values[j]);
        }
            
        printk("\r\n");

    return ret;
}

static void __exit dtsof_exit(void)
{
    printk("\r\n exit dtsof.ko \r\n\r\n");
}


module_init(dtsof_init);
module_exit(dtsof_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("chaoliang");
